package com.xtgg.more;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.xtgg.R;
import com.xtgg.login.LoginActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.PreferencesUtils;
import com.xtgg.util.TLog;
import com.xtgg.util.ToastUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class WarningActivity extends BaseActivity implements OnClickListener
{
	String[] ringType = new String[] { "蜂鸣", "振动","蜂鸣+震动","返回" };
	
	int mDis = 5;
	int mSec = 0;
	int mRing = 0;
	String startTime = "24:00";
	String endTime = "08:00";
	
	int sHour;
	int sMin;
	int eHour;
	int eMin;
	
	SeekBar mSeekBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_warning);
		
		mSeekBar = (SeekBar)findViewById(R.id.sensor_sensitivity);
		mSeekBar.setProgress(1);
		mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
		{
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				if(!arg2) return;
				
				if(arg1 == 0) mSeekBar.setProgress(1);
				if(arg1 == 1) mSeekBar.setProgress(2); 
				if(arg1 == 2) mSeekBar.setProgress(3);
				
				if(mSeekBar.getProgress() == 1) mDis = 5;
				else if(mSeekBar.getProgress() == 2) mDis = 15;
				else mDis = 25;
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				
			}
		});
		
		((TextView) findViewById(R.id.center)).setText("预警设置");
		((Button) findViewById(R.id.right)).setText("完成");
		
		mRing = PreferencesUtils.getInt(WarningActivity.this, "ring_type");
		if(mRing < 0) mRing = 0;

		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);

//		findViewById(R.id.action0).setOnClickListener(this);
		findViewById(R.id.action1).setOnClickListener(this);
		findViewById(R.id.action2).setOnClickListener(this);
		findViewById(R.id.action3).setOnClickListener(this);
		findViewById(R.id.action4).setOnClickListener(this);
		
		((TextView)findViewById(R.id.value0)).setText(mDis + "m");
		((TextView)findViewById(R.id.value1)).setText(mSec + "s");
		((TextView)findViewById(R.id.value2)).setText(ringType[mRing]);
		((TextView)findViewById(R.id.value4)).setText(startTime + " 至 " + endTime);
		
		sendCheckHttpRequest();
	}

	void showDis()
	{
		new AlertDialog.Builder(this).setTitle("列表框").setItems(
				new String[] { "5m","15m","25m","返回" }, new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						if(arg1 < 5)
						{
							mDis = (2*arg1 + 1)*5;
							((TextView)findViewById(R.id.value0)).setText(mDis + "m");
						}
					}
				}).show();
	}
	
	void showSec()
	{
		new AlertDialog.Builder(this).setTitle("列表框").setItems(
				new String[] { "0s","5s", "10s","15s","20s","25s","返回" }, new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						if(arg1 < 6)
						{
							mSec = arg1*5;
							((TextView)findViewById(R.id.value1)).setText(mSec + "s");
						}
					}
				}).show();
	}
	
	void showRing()
	{
		new AlertDialog.Builder(this).setTitle("列表框").setItems(
				ringType, new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						if(arg1 < 3)
						{
							mRing = arg1;
							((TextView)findViewById(R.id.value2)).setText(ringType[mRing]);
						}
					}
				}).show();
	}
	
	@Override
	public void onClick(View arg0) 
	{
		switch(arg0.getId())
		{
		case R.id.left:
			finish();
			break;
			
		case R.id.right:
			sendHttpRequest();
			break;
		
		case R.id.action0:
			showDis();
			break;
			
		case R.id.action1:
			showSec();
			break;
			
		case R.id.action2:
			showRing();
			break;
			
		case R.id.action3:
			if(findViewById(R.id.action4).getVisibility() == View.INVISIBLE)
			{
				findViewById(R.id.action4).setVisibility(View.VISIBLE);
				findViewById(R.id.value5).setVisibility(View.VISIBLE);
				((ImageView)findViewById(R.id.value3)).setImageResource(R.drawable.ic_device_on);
			}
			else
			{
				findViewById(R.id.action4).setVisibility(View.INVISIBLE);
				findViewById(R.id.value5).setVisibility(View.INVISIBLE);
				((ImageView)findViewById(R.id.value3)).setImageResource(R.drawable.ic_device_off);
			}
			break;
			
		case R.id.action4:
			showQuery();
			break;
		}
	}
	
	void showQuery() {
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_time);
		mDialog.findViewById(R.id.left).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						mDialog.dismiss();
					}
				});
		mDialog.findViewById(R.id.right).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						mDialog.dismiss();
						
						if(sHour < 10) startTime = "0" + sHour;
						else startTime = "" + sHour;
						
						startTime += ":";
						if(sMin < 10) startTime += "0" + sMin;
						else startTime += "" + sMin;
						
						if(eHour < 10) endTime = "0" + eHour;
						else endTime = "" + eHour;
						
						endTime += ":";
						if(eMin < 10) endTime += "0" + eMin;
						else endTime += "" + eMin;
						
						((TextView)findViewById(R.id.value4)).setText(startTime + " 至 " + endTime);
					}
				});
		((TimePicker)mDialog.findViewById(R.id.left_time)).setIs24HourView(true);
		((TimePicker)mDialog.findViewById(R.id.right_time)).setIs24HourView(true);

		int seconds = (int)(System.currentTimeMillis()/1000);
		seconds = (seconds + 8*3600)%(24*3600);
		
		sHour = seconds/3600;
		sMin = (seconds - sHour*3600)/60;
		
		eHour = sHour;
		eMin = sMin;
		
		((TimePicker)mDialog.findViewById(R.id.left_time)).setOnTimeChangedListener(new OnTimeChangedListener()
		{
			@Override
			public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
				sHour = arg1;
				sMin = arg2;
			}
		});
		((TimePicker)mDialog.findViewById(R.id.right_time)).setOnTimeChangedListener(new OnTimeChangedListener()
		{
			@Override
			public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
				eHour = arg1;
				eMin = arg2;
			}
		});
		
		mDialog.show();
	}
	
	private void sendCheckHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this,true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);
				if(ruser.getState().equals("1"))
				{
					//{"data":[{"delayed":"5","distance":"5","dormancy":"1","endtime":"08:00","starttime":"24:00","userid":"1000000018"}],"message":"获取用户预警设置成功","state":"1"}
					JSONObject obj = JSON.parseObject(response).getJSONArray("data").getJSONObject(0);
    
					mDis = obj.getIntValue("distance");
					if(mDis == 5) mSeekBar.setProgress(1);
					else if(mDis == 15) mSeekBar.setProgress(2);
					else mSeekBar.setProgress(3);
					
					mSec = obj.getIntValue("delayed");
					
					startTime = obj.getString("starttime");
					endTime = obj.getString("endtime");

					((TextView)findViewById(R.id.value0)).setText(mDis + "m");
					((TextView)findViewById(R.id.value1)).setText(mSec + "s");
					((TextView)findViewById(R.id.value2)).setText(ringType[mRing]);
					((TextView)findViewById(R.id.value4)).setText(startTime + " 至 " + endTime);
					
					int dormancy = obj.getIntValue("dormancy");
					if(dormancy == 0)
					{
						findViewById(R.id.action4).setVisibility(View.VISIBLE);
						((ImageView)findViewById(R.id.value3)).setImageResource(R.drawable.ic_device_on);
					}
				} 
				else if (ruser.getState().equals("2")) 
				{showExitDialog();}
				else 
				{
				}
			}
		};
		// -----------------------

		RequestParams params = new RequestParams();
		params.put("act", "getWarningInfo");
		params.put("userid", PreferencesUtils.getString(this, "userid"));
		MgqRestClient.get(Constants.SETTING, params, loginHandler);
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this,true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);
				if(ruser.getState().equals("1"))
				{
					PreferencesUtils.putInt(WarningActivity.this, "ring_type", mRing);
					ToastUtil.showToastWithAlertPic("设置成功");
				} 
				else if (ruser.getState().equals("2")) 
				{showExitDialog();}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getMessage());
				}
			}
		};
		// -----------------------

		RequestParams params = new RequestParams();
		params.put("act", "addWarningInfo");
		params.put("userid", PreferencesUtils.getString(this, "userid"));
		params.put("distance", String.valueOf(mDis));
		params.put("delayed", String.valueOf(mSec));
		if(findViewById(R.id.action4).getVisibility() == View.INVISIBLE) params.put("dormancy", "1");
		else params.put("dormancy", "0");
		params.put("starttime", startTime);
		params.put("endtime", endTime);
		MgqRestClient.get(Constants.SETTING, params, loginHandler);
	}
}
